﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Threading;
using AutoMapper;
using Sautom.Queries;
using Sautom.Services.Dto;
using Sautom.WcfServer.Interfaces;
using Sautom.WcfServer.TransportDto;

namespace Sautom.WcfServer.Services
{
    //todo T4
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    sealed public partial class FileService : IFileService
    {
        public IFileFinder FileFinder { get; set; }
        public Sautom.Services.FileService FileServiceCommand { get; set; }
        public IClientFinder ClientFinder { get; set; }

        #region Properties
        
        #endregion

        #region Constructors
        public FileService(IFileFinder fileFinder, Sautom.Services.FileService fileService, IClientFinder clientFinder)
        {
            FileFinder = fileFinder;
            FileServiceCommand = fileService;
            ClientFinder = clientFinder;
        }

        #endregion

        #region Implementation of IFileService

        public ICollection<GuidStringDtoOutput> ClientFileList(Guid clientId)
        {
            return Mapper.Map<ICollection<GuidStringDtoOutput>>(FileFinder.ClientFileList(clientId));
        }

        public FileDownloadDtoOutput ClientFile(Guid fileId)
        {
            var file = FileFinder.ClientFile(fileId);
            return new FileDownloadDtoOutput()
                       {
                            Id  = file.Id,
                            FileExtension = file.FileExtension,
                            FileData = file.FileData,
                            FileName = file.FileName
                       };
        }

        public bool UploadFile(ClientFileUploadDtoInput data)
        {
            //Mapper.Map<ClientFileUploadDto>(data);
            var dataMapped = new ClientFileUploadDto()
                                 {
                                     FileData = data.FileData,
                                     ClientId = data.ClientId,
                                     FileName = data.FileName,
                                     FileExtension = data.FileExtension
                                 };

            return FileServiceCommand.UploadFile(dataMapped).IsValid;
        }

        public bool RemoveFile(Guid fileId)
        {
            return FileServiceCommand.RemoveFile(fileId).IsValid;
        }

        public FileDownloadDtoOutput ClientContract(Guid clientId, string type)
        {
            return Mapper.Map<FileDownloadDtoOutput>(ClientFinder.ClientContract(clientId, type));
        }

        #endregion
    }
}
